package com.zmzncs.shop.module.shop.service.impl;

import com.zmzncs.shop.module.shop.mapper.ShopGoodsFileMapper;
import org.springframework.beans.BeanUtils;
import com.zmzncs.shop.module.shop.pojo.shopgoods.*;
import com.zmzncs.shop.module.shop.mapper.ShopGoodsMapper;
import com.zmzncs.shop.module.shop.service.ShopGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 高博文
 * @since 2021-05-25
 */
@Service
public class ShopGoodsServiceImpl extends ServiceImpl<ShopGoodsMapper, ShopGoods> implements ShopGoodsService {

    @Autowired
    private ShopGoodsMapper shopGoodsMapper;
    @Autowired
    ShopGoodsFileMapper shopGoodsFileMapper;

    /**
    * 分页
    */
    @Override
    public IPage<ShopGoodsDTO> page(ShopGoodsSearchForm form){
        IPage<ShopGoodsDTO> iPage = shopGoodsMapper.list(form.getPage(),form);
        if (iPage != null && iPage.getRecords() != null && iPage.getRecords().size() > 0){
            for (ShopGoodsDTO record : iPage.getRecords()) {
                List<String> fileList = shopGoodsFileMapper.getFileByGoodsId(record.getId());
                if (fileList != null && fileList.size() > 0){
                    record.setImageUrl(fileList.get(0));
                }
            }
        }

        return iPage;
    }

    /**
    * 添加
    */
    @Override
    @Transactional
    public void add(ShopGoodsAddForm form){
        ShopGoods shopGoods = new ShopGoods();
        BeanUtils.copyProperties(form, shopGoods);

        shopGoodsMapper.insert(shopGoods);
    }

    /**
    * 删除
    */
    @Override
    @Transactional
    public void delete(Long id){
        ShopGoods shopGoods = new ShopGoods();
        shopGoods.setId(id);
        shopGoods.setIsDeleted(1);

        shopGoodsMapper.updateById(shopGoods);
    }

    /**
    * 修改
    */
    @Override
    @Transactional
    public void update(ShopGoodsUpdateForm form){
        ShopGoods shopGoods = new ShopGoods();
        BeanUtils.copyProperties(form, shopGoods);

        shopGoodsMapper.updateById(shopGoods);
    }

    /**
    * 详情
    */
    @Override
    public ShopGoodsDTO getById(Long id){
        ShopGoodsDTO shopGoodsDTO = shopGoodsMapper.getById(id);

        return shopGoodsDTO;
    }

}
